﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Cubezicles
{
    public class UI : DrawableGameComponent
    {
        const String ModelName = "Models/cubicle";

        Texture2D controlbg, cameraControls, rotate, icon, time, zoom, topbar, bottombar;
        //Rects for the button spritesheets
        Rectangle rotateClockRect, rotateCounterRect,
                    iconRoomRect, iconWorkerRect,
                    pauseRect, playRect, ffRect, fffRect, 
                    zoomInRect, zoomOutRect; 
        //Bounding Box for buttons
        Rectangle BBrotateClockRect, BBrotateCounterRect,
                    BBiconRoomRect, BBiconWorkerRect,
                    BBpauseRect, BBplayRect, BBffRect, BBfffRect,
                    BBzoomInRect, BBzoomOutRect;
        int speedPlay = 1,
            speedFF = 8,
            speedFFF = 64;

        public UI(Game1 game) : base(game) { }

        // Get the Game1 object stored by the base class
        private Game1 game1 { get { return (Game1)Game; } }

        protected override void LoadContent()
        {
            //Load ALL the 2D components
            //Background Components
            controlbg = Game.Content.Load<Texture2D>("UI/controlbg");
            topbar = Game.Content.Load<Texture2D>("UI/topbar");
            bottombar = Game.Content.Load<Texture2D>("UI/bottombar");
            //Buttons
            rotate = Game.Content.Load<Texture2D>("UI/rotate");
            icon = Game.Content.Load<Texture2D>("UI/icon");
            time = Game.Content.Load<Texture2D>("UI/timeall");
            zoom = Game.Content.Load<Texture2D>("UI/zoomall");
            cameraControls = Game.Content.Load<Texture2D>("UI/camera");
            base.LoadContent();
            
        }

        public override void Update(GameTime gameTime)
        {
            //Check for hovering over the buttons then change their rectangles accordingly
            //Set the mouse state
            MouseState ms = Mouse.GetState();
            Rectangle msp = new Rectangle(ms.X, ms.Y, 1, 1);
            //Check collision with rotation
            if (BBrotateClockRect.Intersects(msp)) { 
                rotateClockRect = new Rectangle(0, 53, 30, 50);
                //Check if clicked
                if (ms.LeftButton == ButtonState.Pressed)
                {
                    //Rotate Clockwise
                   // game1.CurrentCam.rotateClock(); .............doesnt exist yet
                }
            }
            else { rotateClockRect = new Rectangle(0, 0, 30, 50); }
            if (BBrotateCounterRect.Intersects(msp)) { 
                rotateCounterRect = new Rectangle(30, 53, 27, 50);
                //Check if clicked
                if (ms.LeftButton == ButtonState.Pressed)
                {
                    //Rotate Counterclockwise
                   // game1.CurrentCam.rotateCounter();................doesnt exist yet
                }
            }
            else { rotateCounterRect = new Rectangle(30, 0, 28, 50); }
            //Check collision with zooming
            if (BBzoomInRect.Intersects(msp)) { 
                zoomInRect = new Rectangle(60, 53, 29, 40);
                //Check if clicked
                if (ms.LeftButton == ButtonState.Pressed)
                {
                    //Zoom in
                    //game1.CurrentCam.zoomIn(); .............doesnt exist yet
                }
            }
            else { zoomInRect = new Rectangle(60, 0, 29, 40); }
            if (BBzoomOutRect.Intersects(msp)) { 
                zoomOutRect = new Rectangle(89, 53, 30, 54);
                //Check if clicked
                if (ms.LeftButton == ButtonState.Pressed)
                {
                    //Zoom Out
                   // game1.CurrentCam.zoomOut(); ............doesnt exist yet
                }
            }
            else { zoomOutRect = new Rectangle(89, 0, 30, 54); }
            //Check collision with icons
            if (BBiconRoomRect.Intersects(msp)) { 
                iconRoomRect = new Rectangle(40, 60, 38, 50);
                //Check if clicked
                if (ms.LeftButton == ButtonState.Pressed)
                {
                    //Bring up the room management Pane
                }
            }
            else { iconRoomRect = new Rectangle(0, 60, 38, 50); }
            if (BBiconWorkerRect.Intersects(msp)) { 
                iconWorkerRect = new Rectangle(40, 0, 38, 54);
                //Check if clicked
                if (ms.LeftButton == ButtonState.Pressed)
                {
                    //Bring up the worker management pane
                }
            }
            else { iconWorkerRect = new Rectangle(0, 0, 38, 54); }
            //Check collision with time
            if (BBpauseRect.Intersects(msp) && game1.timeSpeed != 0) 
            { 
                pauseRect = new Rectangle(0, 24, 25, 25);
                //Check if clicked
                if (ms.LeftButton == ButtonState.Pressed)
                {
                    //Set speed to pause
                    game1.timeSpeed = 0;
                }
            }
            else if (game1.timeSpeed == 0) { pauseRect = new Rectangle(0, 48, 25, 25); }
            else { pauseRect = new Rectangle(0, 0, 25, 25); }

            if (BBplayRect.Intersects(msp) && game1.timeSpeed != speedPlay)
            { 
                playRect = new Rectangle(25, 24, 20, 25);
                //Check if clicked
                if (ms.LeftButton == ButtonState.Pressed)
                {
                    //Set speed to play
                    game1.timeSpeed = speedPlay;
                }
            }
            else if (game1.timeSpeed == speedPlay) { playRect = new Rectangle(25, 24 * 2, 20, 25); }
            else { playRect = new Rectangle(25, 0, 20, 25); }

            if (BBffRect.Intersects(msp) && game1.timeSpeed != speedFF)
            {
                ffRect = new Rectangle(45, 24, 25, 25);
                //Check if clicked
                if (ms.LeftButton == ButtonState.Pressed)
                {
                    //Set speed to fast forward
                    game1.timeSpeed = speedFF;
                }
            }
            else if (game1.timeSpeed == speedFF) { ffRect = new Rectangle(45, 48, 25, 25); }
            else { ffRect = new Rectangle(45, 0, 25, 25); }

            if (BBfffRect.Intersects(msp) && game1.timeSpeed != speedFFF)
            {
                fffRect = new Rectangle(70, 24, 40, 25);
                //Check if clicked
                if (ms.LeftButton == ButtonState.Pressed)
                {
                    //Set speed to fast fast forward
                    game1.timeSpeed = speedFFF;
                }
            }
            else if (game1.timeSpeed == speedFFF) { fffRect = new Rectangle(70, 48, 40, 25); }
            else { fffRect = new Rectangle(70, 0, 40, 25); }

            //Also check for functionality
        }
        public override void Draw(GameTime gameTime)
        {
            //Get the screen width and height
            int screenH = GraphicsDevice.Viewport.Height;
            int screenW = GraphicsDevice.Viewport.Width;

            //Set Bounding boxes
            //Initialize Bounding Boxes
            BBrotateClockRect = new Rectangle(20, screenH - 60, 30, 50);
            BBrotateCounterRect = new Rectangle(85, screenH - 60, 27, 50);
            //arrowRect = new Rectangle(0, 0, 22, 35);
            BBiconRoomRect = new Rectangle(0, screenH - controlbg.Height + 20, 38, 50);
            BBiconWorkerRect = new Rectangle(0, screenH - controlbg.Height + 70, 38, 54);
            BBpauseRect = new Rectangle(150, 3, 25, 25);
            BBplayRect = new Rectangle(150 + playRect.X, 3, 20, 25); //Defaults to selected
            BBffRect = new Rectangle(150 + ffRect.X, 3, 25, 25);
            BBfffRect = new Rectangle(150 + fffRect.X, 3, 40, 25);
            BBzoomInRect = new Rectangle(53, screenH - 80, 29, 40);
            BBzoomOutRect = new Rectangle(53, screenH - 40, 30, 54);

            game1.SpriteBatch.Begin();

            //Draw the Top Bar
            game1.SpriteBatch.Draw(topbar, new Rectangle(0, 0, screenW, topbar.Height), Color.White);
            //Draw the bottom bar
            game1.SpriteBatch.Draw(bottombar, new Rectangle(0, screenH - bottombar.Height, screenW, bottombar.Height), Color.White);
            //Draw the control circle background
            game1.SpriteBatch.Draw(controlbg, new Vector2(0, screenH - controlbg.Height), Color.White);

            //Draw buttons
            game1.SpriteBatch.Draw(cameraControls, BBrotateClockRect, rotateClockRect, Color.White);
            game1.SpriteBatch.Draw(cameraControls, BBrotateCounterRect, rotateCounterRect, Color.White);
            game1.SpriteBatch.Draw(cameraControls, BBzoomInRect, zoomInRect, Color.White);
            game1.SpriteBatch.Draw(cameraControls, BBzoomOutRect, zoomOutRect, Color.White);
            game1.SpriteBatch.Draw(icon, BBiconRoomRect, iconRoomRect, Color.White);
            game1.SpriteBatch.Draw(icon, BBiconWorkerRect, iconWorkerRect, Color.White);
            game1.SpriteBatch.Draw(time, BBpauseRect, pauseRect, Color.White);
            game1.SpriteBatch.Draw(time, BBplayRect, playRect, Color.White);
            game1.SpriteBatch.Draw(time, BBffRect, ffRect, Color.White);
            game1.SpriteBatch.Draw(time, BBfffRect, fffRect, Color.White);

            game1.SpriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
